<?php

namespace app\queue\redis;

use constant\RedisConstant;
use util\RedisTool;
use util\WorkWxTool;
use Webman\RedisQueue\Consumer;

class AssignNoticeSendQueue implements Consumer
{
    public $queue = RedisConstant::AssignNoticeSend;

    //消费
    public function consume($data)
    {
        $taskType = $data['taskType']; //任务类型、标识
        $taskSign = $data['taskSign']; //分派名称、标识、ID
        $info = $data['info']; //数据信息
        $userInfo = $data['userInfo']; //用户信息
        $noticeType = $data['noticeType'] ?? 1; //消息内容
        //派发配置
        $taskSetting = RedisConstant::TaskPrefix . $taskType . ':task:' . $taskSign;
        $setting = RedisTool::get_forever($taskSetting, 'config');

        $message = '<font color="info">**派发通知**</font>
>系统派发了一条数据
>数据ID：' . ($info['id'] ?? '') . '
>用户名称：@' . ($userInfo['username'] ?? '') . '
>请尽快查看！';
        if ($noticeType == 2) {
            $message = '<font color="warning">**派发未查看通知**</font>
>系统将于<font color="warning">**2分钟**</font>之后自动派发给下一个
>数据ID：' . ($info['id'] ?? '') . '
>用户名称：@' . ($userInfo['username'] ?? '');
        }
        if ($noticeType == 3) {
            $message = '<font color="warning">**无可派发人员通知**</font>
<font color="warning">**无可派发人员，派发将与5分钟后重新执行，请尽快处理！**</font>';
        }
        switch ($setting['assignType']) {
            case 1:
                if ($setting['sendAssignUrl']) {
                    $content = ['content' => $message];
                    WorkWxTool::noticeToWork($setting['sendAssignUrl'], 2, $content);
                }
                break;
            default:
                break;
        }

    }

    // 消费失败回调
    public function onConsumeFailure(\Throwable $e, $package)
    {
        WorkWxTool::ErrorMsg('派发消息发送失败', $e->getMessage() . '\n' . json_encode($package['data'], 256));
    }
}